// Package browser implements the browser level jobs and data.
// The browsers allows the user to select the feed, setting, tab and voisfile tools of the browser.
package browser

import (
	"net/http"

	"code.google.com/awwwe-browser/auralvm/bookmark"
	"code.google.com/awwwe-browser/auralvm/conversation"
	"code.google.com/awwwe-browser/auralvm/feed"
	"code.google.com/awwwe-browser/auralvm/param"
	"code.google.com/awwwe-browser/auralvm/setting"
	"code.google.com/awwwe-browser/auralvm/tab"
	"code.google.com/awwwe-browser/auralvm/voisfile"
)

const (
	browserListToolsIntro string = "this is the awe browser. the aural, world wide web, everywhere, browser."
)

// Alternative HTTP handler for sending data back to the view.
var NextContinuer func(http.ResponseWriter, *http.Request, string)
// Final alternative HTTP handler for sending data back to the view.
var StopContinuer func(http.ResponseWriter, *http.Request, string)

// HandlerBrowserListTools lists the browser tools for the view.
func HandlerBrowserListTools(w http.ResponseWriter, r *http.Request) {
	ContinuerBrowserListTools(w, r, browserListToolsIntro, -1, false, true)
}

// ContinuerBrowserListTools continues what HandlerBrowserListTools started.
func ContinuerBrowserListTools(w http.ResponseWriter, r *http.Request, intro string, startoptionsat int, pauseonparse bool, continueon bool) {
	var err error
	var conv *conversation.Conversation
	var option conversation.APIOption
	var jsonbb []byte
	conv = conversation.NewConversation()
	conv.Type = conversation.ConversationTypeOptions
	conv.PauseOnParse = pauseonparse
	conv.Intro = intro
	conv.InitialIndex = startoptionsat
	conv.Header.Append("These are the browser tools.")
	if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
		// a lot of help or a little help.
		// add helpful
		conv.Helpful = " I will iterate through the names of each of the tools. After i say the name of a tool, say yes, if you want to use that tool."
	}
	if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
		// a lot of help
		// add more helpful
		conv.MoreHelpful = " You may say, yes, back, restart, finish, help, pause, or quit."
	}
	conv.Footer = "That is the end of the list of browser tools."
	conv.CallHelp.Package = param.Package
	conv.CallHelp.Object = "browser"
	conv.CallHelp.Method = "listtools"
	// tab tools
	if tab.BrowserTabs.Len() > 0 {
		option = conversation.BuildAPIOption()
		option.VXMLs.Append("Tab tools.")
		option.CallYes.Package = param.Package
		option.CallYes.Object = "tab"
		option.CallYes.Method = "listtools"
		option.CallYes.Arguments[param.ParamSourceObject] = "browser"
		option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
		option.CallYes.Arguments[param.ParamSourceOptionIndex] = "1"
		conv.Options = append(conv.Options, option)
	}
	// book mark tools
	if bookmark.CountBookmarks() > 0 {
		option = conversation.BuildAPIOption()
		option.VXMLs.Append("book mark tools.")
		option.CallYes.Package = param.Package
		option.CallYes.Object = "bookmark"
		option.CallYes.Method = "listtools"
		option.CallYes.Arguments[param.ParamSourceObject] = "browser"
		option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
		option.CallYes.Arguments[param.ParamSourceOptionIndex] = "2"
		conv.Options = append(conv.Options, option)
	}
	// vois.xml tools
	if voisfile.CountVOISFiles() > 0 {
		option = conversation.BuildAPIOption()
		option.VXMLs.Append("vois tools.")
		option.CallYes.Package = param.Package
		option.CallYes.Object = "vois"
		option.CallYes.Method = "listtoopen"
		option.CallYes.Arguments[param.ParamSourceObject] = "browser"
		option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
		option.CallYes.Arguments[param.ParamSourceOptionIndex] = "3"
		conv.Options = append(conv.Options, option)
	}
	// feeds
	if feed.CountFeeds() > 0 {
		option = conversation.BuildAPIOption()
		option.VXMLs.Append("web site syndication tools.")
		option.CallYes.Package = param.Package
		option.CallYes.Object = "feed"
		option.CallYes.Method = "listtools"
		option.CallYes.Arguments[param.ParamSourceObject] = "browser"
		option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
		option.CallYes.Arguments[param.ParamSourceOptionIndex] = "4"
		conv.Options = append(conv.Options, option)
	}
	// settings
	option = conversation.BuildAPIOption()
	option.VXMLs.Append("settings tools.")
	option.CallYes.Package = param.Package
	option.CallYes.Object = "settings"
	option.CallYes.Method = "listtools"
	option.CallYes.Arguments[param.ParamSourceObject] = "browser"
	option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
	option.CallYes.Arguments[param.ParamSourceOptionIndex] = "0"
	conv.Options = append(conv.Options, option)

	// serialize the conversation
	jsonbb, err = conv.Marshal()
	if err == nil {
		w.Write(jsonbb)
	} else {
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			StopContinuer(w, r, intro)
		}
	}
}

// HandlerBrowserQuit handles quitting for the view.
func HandlerBrowserQuit(w http.ResponseWriter, r *http.Request) {
	tab.BrowserTabs.DeleteAll()
	ContinuerBrowserListTools(w, r, browserListToolsIntro, -1, true, false)
}
